package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.FilterGridAdapter;
import com.szgr.baoyin.model.FilterItem;
import com.szgr.baoyin.utils.MyTool;
import com.szgr.baoyin.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class FilterAccessoryActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, AdapterView.OnItemClickListener {

    private RadioButton rb_first;
    private RadioButton rb_second;
    private RadioButton rb_third;
    private RadioButton rb_fourth;
    private List<FilterItem> dataFirst;
    private List<FilterItem> dataSecond;
    private List<FilterItem> dataThird;
    private List<FilterItem> dataFourth;
    private FilterGridAdapter gridAdapter;
    private int pos = 0;
    private FilterItem filterItem;
    private EditText et_lowPrice;
    private EditText et_highPrice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_filter);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("筛选");
    }

    @Override
    public void initView() {
        initData();
        et_lowPrice = (EditText) findViewById(R.id.filter_low_price);
        et_highPrice = (EditText) findViewById(R.id.filter_high_price);
        RadioGroup rg_all = (RadioGroup) findViewById(R.id.accessory_radio_group);
        rg_all.setOnCheckedChangeListener(this);
        rb_first = (RadioButton) findViewById(R.id.accessory_first);
        rb_second = (RadioButton) findViewById(R.id.accessory_second);
        rb_third = (RadioButton) findViewById(R.id.accessory_third);
        rb_fourth = (RadioButton) findViewById(R.id.accessory_fourth);
        findViewById(R.id.sure_back).setOnClickListener(this);

        GridView gridView = (GridView) findViewById(R.id.filter_grid_view);
        gridAdapter = new FilterGridAdapter(this, dataFirst);
        gridView.setAdapter(gridAdapter);
        gridView.setOnItemClickListener(this);

        Intent intent = getIntent();
        if(intent.hasExtra("position")){
            int position = intent.getIntExtra("position",0);
            switch (position){
                case 0:
                    rb_first.setChecked(true);
                    changeTextColor(rb_first, rb_second, rb_third, rb_fourth);
                    gridAdapter.setData(dataFirst);
                    pos = 0;
                    break;
                case 1:
                    rb_second.setChecked(true);
                    changeTextColor(rb_second, rb_first, rb_third, rb_fourth);
                    gridAdapter.setData(dataSecond);
                    pos = 1;
                    break;
                case 2:
                    rb_third.setChecked(true);
                    changeTextColor(rb_third, rb_second,rb_first , rb_fourth);
                    gridAdapter.setData(dataThird);
                    pos = 2;
                    break;
                case 3:
                    rb_fourth.setChecked(true);
                    changeTextColor(rb_fourth, rb_second, rb_third, rb_first);
                    gridAdapter.setData(dataFourth);
                    pos = 3;
                    break;
            }
        }

    }

    private void initData() {
        dataFirst = new ArrayList<>();
        dataSecond = new ArrayList<>();
        dataThird = new ArrayList<>();
        dataFourth = new ArrayList<>();
        String[] first = new String[]{"刀具","轴承","第四轴","排屑机","变压器","水泵","打刀缸","手轮","照明灯","空气开关","断路器","热过载保护器",
                "主轴","丝杆","线轨滑块","油冷机","热交换器","稳压器","润滑泵","面板","三色灯","电磁阀","油水分离器","交流接触器","继电器"};
        for (String str : first) {
            dataFirst.add(new FilterItem(str));
        }
        dataSecond.add(new FilterItem("刀具"));
        dataSecond.add(new FilterItem("刀柄"));
        String[] third = new String[]{"凸模","凹模","导正销","卸料板导柱","模架用导数","独立导柱组件","螺旋弹簧","小零件"};
        for (String str : third) {
            dataThird.add(new FilterItem(str));
        }
        dataFourth.add(new FilterItem("设备润滑油"));
        dataFourth.add(new FilterItem("金属加工油"));
        dataFourth.add(new FilterItem("特种润滑油"));
        dataFourth.add(new FilterItem("工业润滑油"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.sure_back:
                if(filterItem != null) {
                    Intent intent = new Intent();
                    intent.putExtra("position", pos);
                    intent.putExtra("low", MyTool.getText(et_lowPrice));
                    intent.putExtra("high",MyTool.getText(et_highPrice));
                    intent.putExtra("filter",filterItem.getName());
                    setResult(309,intent);
                    finish();
                }else{
                    ToastUtils.showToast("点击选择一个类型");
                }
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.accessory_first:
                changeTextColor(rb_first, rb_second, rb_third, rb_fourth);
                gridAdapter.setData(dataFirst);
                pos = 0;
                break;
            case R.id.accessory_second:
                changeTextColor(rb_second, rb_first, rb_third, rb_fourth);
                gridAdapter.setData(dataSecond);
                pos = 1;
                break;
            case R.id.accessory_third:
                changeTextColor(rb_third, rb_second,rb_first , rb_fourth);
                gridAdapter.setData(dataThird);
                pos = 2;
                break;
            case R.id.accessory_fourth:
                changeTextColor(rb_fourth, rb_second, rb_third, rb_first);
                gridAdapter.setData(dataFourth);
                pos = 3;
                break;
        }
    }

    private void changeTextColor(RadioButton rb1, RadioButton rb2, RadioButton rb3, RadioButton rb4) {
        rb1.setTextColor(0xffffffff);
        rb2.setTextColor(0xff234b64);
        rb3.setTextColor(0xff234b64);
        rb4.setTextColor(0xff234b64);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (pos){
            case 0:
                filterItem = dataFirst.get(position);
                for (FilterItem fi : dataFirst) {
                    fi.setIsChecked(false);
                }
                break;
            case 1:
                filterItem = dataSecond.get(position);
                for (FilterItem fi : dataSecond) {
                    fi.setIsChecked(false);
                }
                break;
            case 2:
                filterItem = dataThird.get(position);
                for (FilterItem fi : dataThird) {
                    fi.setIsChecked(false);
                }
                break;
            case 3:
                filterItem = dataFourth.get(position);
                for (FilterItem fi : dataFourth) {
                    fi.setIsChecked(false);
                }
                break;
        }
        filterItem.setIsChecked(true);
        gridAdapter.notifyDataSetChanged();
    }
}
